#!/usr/bin/perl
BEGIN {
    my $base_module_dir = (-d '/home/erep/perl' ? '/home/erep/perl' : ( getpwuid($>) )[7] . '/perl/');
    unshift @INC, map { $base_module_dir . $_ } @INC;
}
print "Content-type: text/html\n\n";

@LIST=`curl \"http://erep.tw/etaiwan_ftw/list.php\"`;
$list_num=0;
while ($line = $LIST[$list_num])
{
	$line =~ s/ //g;
	$line =~ s/\n//;
	$line_num=0;
    $id=$line;
	print "$id\n";
	@FD=`curl \"http://www.erepublik.com/en/citizen/profile/$id\"`;
	while ($line = $FD[$line_num])
	{
		if(index($line,"Strength") >= 0)
		{
#ignore lines
			$line_num=$line_num+3;
			$line = $FD[$line_num];
			@str= split(/ /, $line);
		} 
		elsif(index($line,"Rank points") >= 0)
		{
#ignore lines
			$line_num=$line_num+21;
			$line = $FD[$line_num];
			@temp= split(/>/, $line);
			@rankex= split(/ /, $temp[2]);
		}
		elsif(index($line,"citizen_avatar") >= 0)
		{
#ignore lines
			$line_num=$line_num+4;
			$line = $FD[$line_num];
			@name= split(/"/, $line);
			
		}
		elsif(index($line,"background-image") >= 0)
		{
#ignore lines
			$line_num=$line_num+1;
			$line = $FD[$line_num];
			@temp= split(/Citizens/, $line);
			@avatar= split(/_/, $temp[1]);
		}
		$line_num=$line_num+1; 
	}
	$rankex[0] =~ s/,//;
	$rankex[0] =~ s/,//;
	$name[1] =~ s/ /%20/g;
	$str[8] =~ s/,//;
	if ($rankex[0]<10)
	{
		$title="Recruit";
		$rank=1;
	}
	elsif ($rankex[0]>=10 && $rankex[0]<30)
	{
		$title="Private";
		$rank=2;
	}
	elsif ($rankex[0]>=30 && $rankex[0]<60)
	{
		$title="Private*";
		$rank=3;
	}
	elsif ($rankex[0]>=60 && $rankex[0]<100)
	{
		$title="Private**";
		$rank=4;
	}
	elsif ($rankex[0]>=100 && $rankex[0]<150)
	{
		$title="Private***";
		$rank=5;
	}
	elsif ($rankex[0]>=150 && $rankex[0]<250)
	{
		$title="Corporal";
		$rank=6;
	}
	elsif ($rankex[0]>=250 && $rankex[0]<350)
	{
		$title="Corporal*";
		$rank=7;
	}
	elsif ($rankex[0]>=350 && $rankex[0]<450)
	{
		$title="Corporal**";
		$rank=8;
	}
	elsif ($rankex[0]>=450 && $rankex[0]<600)
	{
		$title="Corporal***";
		$rank=9;
	}
	elsif ($rankex[0]>=600 && $rankex[0]<800)
	{
		$title="Sergeant";
		$rank=10;
	}
	elsif ($rankex[0]>=800 && $rankex[0]<1000)
	{
		$title="Sergeant*";
		$rank=11;
	}
	elsif ($rankex[0]>=1000 && $rankex[0]<1400)
	{
		$title="Sergeant**";
		$rank=12;
	}
	elsif ($rankex[0]>=1400 && $rankex[0]<1850)
	{
		$title="Sergeant***";
		$rank=13;
	}
	elsif ($rankex[0]>=1850 && $rankex[0]<2350)
	{
		$title="Lieutenant";
		$rank=14;
	}
	elsif ($rankex[0]>=2350 && $rankex[0]<3000)
	{
		$title="Lieutenant*";
		$rank=15;
	}
	elsif ($rankex[0]>=3000 && $rankex[0]<3750)
	{
		$title="Lieutenant**";
		$rank=16;
	}
	elsif ($rankex[0]>=3750 && $rankex[0]<5000)
	{
		$title="Lieutenant***";
		$rank=17;
	}
	elsif ($rankex[0]>=5000 && $rankex[0]<6500)
	{
		$title="Captain";
		$rank=18;
	}
	elsif ($rankex[0]>=6500 && $rankex[0]<9000)
	{
		$title="Captain*";
		$rank=19;
	}
	elsif ($rankex[0]>=9000 && $rankex[0]<12000)
	{
		$title="Captain**";
		$rank=20;
	}
	elsif ($rankex[0]>=12000 && $rankex[0]<15500)
	{
		$title="Captain***";
		$rank=21;
	}
	elsif ($rankex[0]>=15500 && $rankex[0]<20000)
	{
		$title="Major";
		$rank=22;
	}
	elsif ($rankex[0]>=20000 && $rankex[0]<25000)
	{
		$title="Major*";
		$rank=23;
	}
	elsif ($rankex[0]>=25000 && $rankex[0]<31000)
	{
		$title="Major**";
		$rank=24;
	}
	elsif ($rankex[0]>=31000 && $rankex[0]<40000)
	{
		$title="Major***";
		$rank=25;
	}
	elsif ($rankex[0]>=40000 && $rankex[0]<52000)
	{
		$title="Commander";
		$rank=26;
	}
	elsif ($rankex[0]>=52000 && $rankex[0]<67000)
	{
		$title="Commander*";
		$rank=27;
	}
	elsif ($rankex[0]>=67000 && $rankex[0]<85000)
	{
		$title="Commander**";
		$rank=28;
	}
	elsif ($rankex[0]>=85000 && $rankex[0]<110000)
	{
		$title="Commander***";
		$rank=29;
	}
	elsif ($rankex[0]>=110000 && $rankex[0]<140000)
	{
		$title="LtColonel";
		$rank=30;
	}
	elsif ($rankex[0]>=140000 && $rankex[0]<180000)
	{
		$title="LtColonel*";
		$rank=31;
	}
	elsif ($rankex[0]>=180000 && $rankex[0]<225000)
	{
		$title="LtColonel**";
		$rank=32;
	}
	elsif ($rankex[0]>=225000 && $rankex[0]<285000)
	{
		$title="LtColonel***";
		$rank=33;
	}
	elsif ($rankex[0]>=285000 && $rankex[0]<355000)
	{
		$title="Colonel";
		$rank=34;
	}
	elsif ($rankex[0]>=355000 && $rankex[0]<435000)
	{
		$title="Colonel*";
		$rank=35;
	}
	elsif ($rankex[0]>=435000 && $rankex[0]<540000)
	{
		$title="Colonel**";
		$rank=36;
	}
	elsif ($rankex[0]>=540000 && $rankex[0]<660000)
	{
		$title="Colonel***";
		$rank=37;
	}
	elsif ($rankex[0]>=660000 && $rankex[0]<800000)
	{
		$title="General";
		$rank=38;
	}
	elsif ($rankex[0]>=800000 && $rankex[0]<950000)
	{
		$title="General*";
		$rank=39;
	}
	elsif ($rankex[0]>=950000 && $rankex[0]<1140000)
	{
		$title="General**";
		$rank=40;
	}
	elsif ($rankex[0]>=1140000 && $rankex[0]<1350000)
	{
		$title="General***";
		$rank=41;
	}
	elsif ($rankex[0]>=1350000 && $rankex[0]<1600000)
	{
		$title="FieldMarshal";
		$rank=42;
	}
	elsif ($rankex[0]>=1600000 && $rankex[0]<1875000)
	{
		$title="FieldMarshal*";
		$rank=43;
	}
	elsif ($rankex[0]>=1875000 && $rankex[0]<2185000)
	{
		$title="FieldMarshal**";
		$rank=44;
	}
	elsif ($rankex[0]>=2185000 && $rankex[0]<2250000)
	{
		$title="FieldMarshal***";
		$rank=45;
	}
	elsif ($rankex[0]>=2250000 && $rankex[0]<3000000)
	{
		$title="SupremeMarshal";
		$rank=46;
	}
	elsif ($rankex[0]>=3000000 && $rankex[0]<3500000)
	{
		$title="SupremeMarshal*";
		$rank=47;
	}
	elsif ($rankex[0]>=3500000 && $rankex[0]<4150000)
	{
		$title="SupremeMarshal**";
		$rank=48;
	}
	elsif ($rankex[0]>=4150000 && $rankex[0]<4900000)
	{
		$title="SupremeMarshal***";
		$rank=49;
	}
	elsif ($rankex[0]>=4900000 && $rankex[0]<5800000)
	{
		$title="NationalForce";
		$rank=50;
	}
	elsif ($rankex[0]>=5800000 && $rankex[0]<7000000)
	{
		$title="NationalForce*";
		$rank=51;
	}
	elsif ($rankex[0]>=7000000 && $rankex[0]<9000000)
	{
		$title="NationalForce**";
		$rank=52;
	}
	elsif ($rankex[0]>=9000000 && $rankex[0]<11500000)
	{
		$title="NationalForce***";
		$rank=53;
	}
	elsif ($rankex[0]>=11500000 && $rankex[0]<14500000)
	{
		$title="World ClassForce";
		$rank=54;
	}
	elsif ($rankex[0]>=14500000 && $rankex[0]<18000000)
	{
		$title="WorldClassForce*";
		$rank=55;
	}
	elsif ($rankex[0]>=18000000 && $rankex[0]<22000000)
	{
		$title="WorldClassForce**";
		$rank=56;
	}
	elsif ($rankex[0]>=22000000 && $rankex[0]<26500000)
	{
		$title="WorldClassForce***";
		$rank=57;
	}
	elsif ($rankex[0]>=26500000 && $rankex[0]<31500000)
	{
		$title="LegendaryForce";
		$rank=58;
	}
	elsif ($rankex[0]>=31500000 && $rankex[0]<37000000)
	{
		$title="LegendaryForce*";
		$rank=59;
	}
	elsif ($rankex[0]>=37000000 && $rankex[0]<43000000)
	{
		$title="LegendaryForce**";
		$rank=60;
	}
	elsif ($rankex[0]>=43000000 && $rankex[0]<50000000)
	{
		$title="LegendaryForce***";
		$rank=61;
	}
	elsif ($rankex[0]>=50000000 && $rankex[0]<60000000)
	{
		$title="GodofWar";
		$rank=62;
	}
	$strength = sprintf("%f", $str[8]);
	$ranknum = sprintf("%d", $rank);
	$influence = (($ranknum - 1)/20 + 0.3) * (($strength / 10) + 40);
    $rankexp=$rankex[0];

    $update= sprintf("http://erep.tw/etaiwan_ftw/perl.php?Name=%s\&ID=%s\&Strength=%s\&MilitaryRank=%d\&MilitaryEXP=%d\&avatar=%s",$name[1],$id,$str[8],$rank,$rankexp,$avatar[0]);
    `curl \"$update\"`; 
	sleep 0.5;
    $list_num=$list_num+1; 
}
close (LIST);
print "Finish!!\n"; 